local t_mana = {}
local w_mana = require "libs/worldmanager"
local bf_util = require "data/buffutil"
local w_util = require "libs/worldutil"
local bs_util = require "data/bossutil"
local t_util = require "libs/tableutil"
local h_util = require "libs/hudutil"
local changed = false
local data = w_mana:GetWorldDataMap("huxi_timer")

-- 玩家进入游戏时加载乱七八糟数据时也有时间，要处理掉这部分时间
local function InitData()
    t_util:Pairs(data, function(id, timer)
        local tp = timer.type
        if tp == "buff" then
            timer.lft = timer.lft - 3
        elseif tp == "boss" then
        else
        end
    end)
end
InitData()

function t_mana:AddBuff(name, image, altas, left_time, description)
    if name and type(image)=="string" and type(left_time)=="number" then
        local id = "buff_"..name
        if not data[id] then
            self:SetUIRefresh(true)
        end
        -- 科雷代码的bug，新的buff会替代旧的buff，哪怕时长短的buff也会替代时长更长的buff
        data[id] = {
            type = "buff",
            img = image,
            als = altas,
            lft = left_time,
            des = description,
            bir = w_util:GetTimePlaying()              -- 此项数据并没有存储的意义
        }
    else
        print("无法添加BUFF，参数错误！")
    end
end

function t_mana:AddBoss(name, image, altas, duration, description)
    if name and type(image)=="string" and type(duration)=="number" then
        local id = "boss_"..name
        if not data[id] then
            self:SetUIRefresh(true)
        end
        data[id] = {
            type = "boss",
            img = image,
            als = altas,
            des = description,
            ovr = w_util:GetWorldTime()+duration,
        }
    else
        print("无法添加BOSS，参数错误！")
    end
end

function t_mana:AddFoodTimer(foodname)
    t_util:Pairs(bf_util:GetBuffs(foodname), function(name, buff)
        self:AddBuff(name, buff.image, buff.altas, buff.duration, buff.describe)
    end)
end

function t_mana:AddBossTimer(bossname)
    t_util:Pairs(bs_util:GetBoses(bossname), function(name, boss)
        self:AddBoss(name, boss.image, boss.altas, boss.duration, boss.descripe)
    end)
end


w_mana:AddPlayerLeaveFunc(function()
    local time_playing = w_util:GetTimePlaying()
    t_util:Pairs(data, function(id, timer)
        local tp = timer.type
        if tp == "buff" then
            data[id].lft = (timer.bir or 0) + timer.lft - time_playing
            data[id].bir = nil
        elseif tp == "boss" then
            -- 因为下次刷新时间是确定的，所以无需更新
        else
        end
    end)
end)

-- 移除某个倒计时数据时应该做什么
function t_mana:RemoveTimer(id)
    if id then
        data[id] = nil
    end
    self:SetUIRefresh(true)
end


function t_mana:SetUIRefresh(bool)
    changed = bool
end

-- 这个接口用来通知是否刷新所有ui，而不是仅刷新文本
function t_mana:GetUIRefresh()
    return changed
end

-- 将倒计时数据转换为图标
function t_mana:GetTimers()
    local timers = {
        buff = {},
        boss = {},
    }
    local time_playing = w_util:GetTimePlaying()
    local time_now = w_util:GetWorldTime()

    t_util:Pairs(data, function(id, timer)
        local tp = timer.type
        if tp == "buff" then
            local left_time = (timer.bir or 0) + timer.lft - time_playing
            if left_time > 0 then
                timers.buff[id]={
                    atlas = timer.als or h_util:GetPrefabAsset(timer.img),
                    image = timer.als and timer.img or timer.img..".tex",
                    text = w_util:FormatTime(left_time),
                    color = left_time>60 and UICOLOURS.WHITE or UICOLOURS.RED,
                    description = timer.des or timer.text,
                }
            else
                self:RemoveTimer(id)
            end
        elseif tp == "boss" then
            local left_time = timer.ovr - time_now
            if left_time > 0 then
                timers.boss[id]={
                    atlas = timer.als or h_util:GetPrefabAsset(timer.img),
                    image = timer.als and timer.img or timer.img..".tex",
                    text = w_util:FormatTime(left_time),
                    color = left_time>60 and UICOLOURS.WHITE or UICOLOURS.RED,
                    description = timer.des or timer.text,
                }
            else
                self:RemoveTimer(id)
            end
        else
        end
    end)


    
    -- TODO：或许这里可以将各种buff排序？
    return timers
end

-- 这里是核心, 所有发送的事件在这里处理
function t_mana:Process(event)
    if type(event)~= "table" then return end
    local tp,dd = event.type, event.data
    if type(tp)~="string" then return end
    if tp == "EatFood" then
        t_mana:AddFoodTimer(dd)
    elseif tp == "BossAppear" then
        -- local name = dd
        -- local boss_data = bs_util:LoadPrefabData(name)
        -- local one = boss_data[name]
        -- -- 没有死亡判定的boss才会移除倒计时
        -- if one and not one.func_death then
        --     local boses = bs_util:GetBoses(name)
        --     for name, boss in pairs(boses)do
        --         t_mana:RemoveTimer(name)
        --     end
        -- end
    elseif tp == "BossDeath" then
        t_mana:AddBossTimer(dd)
    end
end

-- 重新选择人物
function t_mana:ResetPlayer()
    t_util:Pairs(data, function(id, timer)
        local tp = timer.type
        if tp == "buff" then
            t_mana:RemoveTimer(id)
        elseif tp == "boss" then
        else
        end
    end)
end
-- timer
-- {img, (als), (des), type, }
return t_mana